<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎注册</title>
    <link href="../css/register.css" rel="stylesheet">
</head>
<body>

<div class="form-div">
    <div class="reg-content">
        <h1>欢迎注册</h1>
        <span>已有帐号？</span> <a href="#">登录</a>
    </div>
    <form id="reg-form" action="#" method="post">

        <table>

            <tr>
                <td>用户名</td>
                <td class="inputs">
                    <input name="username" type="text" id="username">
                    <br>
                    <span id="username_err" class="err_msg" style="display: none">用户名不太受欢迎</span>
                </td>

            </tr>

            <tr>
                <td>密码</td>
                <td class="inputs">
                    <input name="password" type="password" id="password">
                    <br>
                    <span id="password_err" class="err_msg" style="display: none">密码格式有误</span>
                </td>
            </tr>


            <tr>
                <td>手机号</td>
                <td class="inputs"><input name="tel" type="text" id="tel">
                    <br>
                    <span id="tel_err" class="err_msg" style="display: none">手机号格式有误</span>
                </td>
            </tr>

        </table>

        <div class="buttons">
            <input value="注 册" type="submit" id="reg_btn">
        </div>
        <br class="clear">
    </form>

</div>


<script>

    //1. 验证用户名是否符合规则
    //1.1 获取用户名的输入框
    var elementById = document.getElementById('username');
    //1.2 绑定onblur事件 失去焦点
    elementById.onblur = checkName;

    function checkName() {
        //1.2 获取用户输入用户名
        let username = elementById.value.trim();

        //1.3 判断用户名是否符合规则 长度6~10
       // let falg = username.length <= 10 && username.length >= 6;
        let reg=/^\w{6,10}$/;
        let  falg=reg.test(username);
        if (falg) {
            document.getElementById('username_err').style.display = 'none';
        } else {
            document.getElementById('username_err').style.display = '';
        }
        return falg;
    }

    /*    var passwordInput = document.getElementById('password');
        //1.2 绑定onblur事件 失去焦点
        passwordInput.onblur =checkpassword;
        function checkpassword() {
            //1.2 获取用户输入用户名
            var password = passwordInput.value.trim();

            //1.3 判断用户名是否符合规则 长度6~10
            var falg=password.length<=10 && password.length>=6;
            if (falg){
                document.getElementById('password_err').style.display='none';
            }else {
                document.getElementById('password_err').style.display='';
            }
            return falg;
        }*/
    //1. 验证密码是否符合规则
    //1.1 获取密码的输入框
    var passwordInput = document.getElementById('password');
    //1.2 绑定onblur事件 失去焦点
    passwordInput.onblur = checkPassword;

    function checkPassword() {
        let passWord = passwordInput.value.trim();
       // let flag = passWord.length >= 6 && passWord.length <= 10;
        let reg=/^\w{6,10}$/;
        let  falg=reg.test(passWord);
        if (falg) {
            document.getElementById('password_err').style.display = 'none'
        } else {
            document.getElementById('password_err').style.display = '';
        }
        return falg;
    }

    //1. 手机号码是否符合规则
    var telInput = document.getElementById('tel');
    //1.2 绑定onblur事件 失去焦点
    telInput.onblur = checkTel;

    function checkTel() {
        let tel = telInput.value.trim();
      //  let flag = tel.length == 11;
        let reg=/^[1]\d{10}$/;
        let flag=reg.test(tel);
        if (flag) {
            document.getElementById('tel_err').style.display = 'none';
        } else {
            document.getElementById('tel_err').style.display = '';
        }
        return flag;
    }

    //1. 获取表单对象
    var form = document.getElementById('reg-form+++++++++++++++++++');
    //2. 绑定onsubmit 事件l
    form.onsubmit = function () {
        return falg = checkName() && checkPassword() && checkTel();
    };


</script>
</body>
</html>